package cc.mrbird.febs.system.controller;


import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.domain.FebsResponse;
import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.system.domain.THouse;
import cc.mrbird.febs.system.service.ITHouseService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author dtwei
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/tHouse")
public class THouseController extends BaseController {

    private String message;

    @Resource
    private ITHouseService houseService;

    @GetMapping
    @RequiresPermissions("house:view")
    public Map<String, Object> houseList(QueryRequest queryRequest, THouse tHouse, String areaName) {
        return getDataTable(this.houseService.findHouses(tHouse, areaName, queryRequest));
    }

    @Log("新增住宅")
    @PostMapping
    @RequiresPermissions("house:add")
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse addHouse(@RequestBody @Valid THouse tHouse) throws FebsException {
        try {
            this.houseService.createHouse(tHouse);
            return new FebsResponse().code("200").message("新增住宅成功").status("success");
        } catch (FebsException e) {
            throw e;
        } catch (Exception e) {
            message = "新增住宅失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除住宅")
    @DeleteMapping("/{houseIds}")
    @RequiresPermissions("house:delete")
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse deleteHouses(@NotBlank(message = "{required}") @PathVariable String houseIds) throws FebsException {
        try {
            String[] ids = houseIds.split(StringPool.COMMA);
            this.houseService.deleteHouses(ids);
            return new FebsResponse().code("200").message("删除住宅成功").status("success");
        } catch (FebsException e) {
            throw e;
        } catch (Exception e) {
            message = "删除住宅失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除住宅")
    @DeleteMapping("/deleteHouse")
    @RequiresPermissions("house:delete")
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse deleteHousesByIds(@NotBlank(message = "{required}") String houseIds) throws FebsException {
        try {
            String[] ids = houseIds.split(StringPool.COMMA);
            this.houseService.deleteHouses(ids);
            return new FebsResponse().code("200").message("删除住宅成功").status("success");
        } catch (FebsException e) {
            throw e;
        } catch (Exception e) {
            message = "删除住宅失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改住宅")
    @PutMapping
    @RequiresPermissions("house:update")
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse updateHouse(@RequestBody @Valid THouse tHouse) throws FebsException {
        try {
            this.houseService.updateHouse(tHouse);
            return new FebsResponse().code("200").message("修改住宅成功").status("success");
        } catch (FebsException e) {
            throw e;
        } catch (Exception e) {
            message = "修改住宅失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
